(function (win) {
  var doc = win.document,
    html = doc.documentElement,
    scale = 16,
    resizeEvent =
      "orientationchange" in window ? "orientationchange" : "resize";
  function calculate() {
    windowWidth = (html && html.clientWidth) || win.innerWidth;
    windowHeight = (html && html.clientHeight) || win.innerHeight;
    var screenChange = document.getElementById("screen-change");
    if (windowWidth < windowHeight) {
      screenChange.classList.remove("h");
      screenChange.classList.add("w");
    } else {
      screenChange.classList.remove("w");
      screenChange.classList.add("h");
    }
    deviceAgent = navigator.userAgent.toLowerCase();
    scale = parseFloat(windowWidth / 3.2);
    // 修复微信2.3系统bug
    if (
      deviceAgent.match(/android\s*2.3/) &&
      deviceAgent.match(/micromessenger/)
    ) {
      scale = 16;
    }
    if (scale > 150) {
      scale = 150;
    }
    html.style.fontSize = scale + "px";
    console.log("font size:", scale, "px");
  }
  html.style.opacity = 0; // 未渲染出来先把页面隐藏
  win.addEventListener(
    resizeEvent,
    function () {
      calculate();
    },
    false
  );
  doc.addEventListener(
    "DOMContentLoaded",
    function () {
      calculate();
      body = doc.body;
      body.style.width = "3.2rem";
      body.style.fontSize = "0.13rem";
      body.style.margin = "0px auto";
      html.style.opacity = 1;
    },
    false
  );
})(window);
